package com.app.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @ClassName BetweenTimeUtils
 * @Description TODO
 * @Author yoominic
 * @Date 2022/8/26 18:00 周五
 * @Version 1.0.0
 **/
public class BetweenTimeUtils {

    /***
     * description: 时间间隔计算格式化工具
     * author: yoominic
     * date: 2022/8/26 18:00
     *
     * @param time
     * @return java.lang.String
     */
    public static String BetweenUtils(Date time) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String createDate = null;
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String createTime = sdf2.format(time);
        System.out.println("createTime: " + createTime);
        Date date = null;
        try {
            date = sdf2.parse(createTime);
            long differenceValue = System.currentTimeMillis() - date.getTime();
            if (differenceValue < 3600000) {
                if ((differenceValue / 1000 / 60) == 0) {
                    createDate = "刚刚";
                } else {
                    createDate = (differenceValue / 1000 / 60) + "分钟前";
                }

            } else if (differenceValue >= 3600000 && differenceValue < 86400000) {
                if (differenceValue < 86400000) {
                    createDate = (differenceValue / 1000 / 60 / 60) + "小时前";
                } else {
                    createDate = sdf.format(time);
                }
            } else if (differenceValue >= 86400000) {
                createDate = (differenceValue / 1000 / 60 / 60 / 60) + "天前";
            }
            return createDate;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

}
